package root.electronclass.electronclass.common.base;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.ToastUtils;

import root.electronclass.electronclass.common.dialog.HttpLoadingDialog;
import root.electronclass.electronclass.common.dialog.K12DialogHelper;
import root.electronclass.electronclass.mvp.base.DialogViewModel;


public class BaseFragment extends Fragment {
    public ProgressDialog progress;
    public K12DialogHelper helper;
    public HttpLoadingDialog httpDialog;
    protected DialogViewModel mDialogViewModel;

    private DialogViewModel getViewModel() {
        return new ViewModelProvider(
                this,
                new ViewModelProvider.Factory() {
                    @NonNull
                    @Override
                    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
                        return (T) new DialogViewModel();
                    }
                })
                .get(DialogViewModel.class);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialogViewModel = getViewModel();
    }

    @SuppressLint("FragmentLiveDataObserve")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mDialogViewModel.showProgressData.observe(
                this,
                showProgress -> {
                    onCompelet();
                    if (showProgress) showWaitingMessage();
                    else dismissProgress();
                });
        mDialogViewModel.showErrorData.observe(
                this,
                baseResponse -> {
                    onCompelet();
                    dismissProgress();
                    toast(baseResponse.getMsg());

                });
        mDialogViewModel.showExceptionData.observe(
                this,
                throwable -> {
                    toast(throwable.getMessage());
                    dismissProgress();
                    onCompelet();
                });
    }

    public void showWaitingMessage() {
//        if (httpDialog == null) {
//            httpDialog = new HttpLoadingDialog(getActivity(), getString(R.string.loading_wait));
//            httpDialog.setCanceledOnTouchOutside(false);
//            httpDialog.setOnCancelListener(dialog -> getActivity().finish());
//            httpDialog.show();
//        } else {
//            httpDialog.show();
//        }
    }

    public void showProgress(String title, String msg, boolean isCancelable) {
//        if (progress == null) {
//            progress = ProgressDialog.show(getActivity(), title, msg, true, isCancelable);
//            progress.setCanceledOnTouchOutside(false);
//            progress.setOnCancelListener(dialog -> getActivity().finish());
//        } else progress.show();
    }

    public void dismissProgress() {
//        if (progress != null) progress.dismiss();
//        if (httpDialog != null) {
//            httpDialog.dismiss();
//        }
    }

    public void dismissProgress(int pro) {
//        if (progress != null) progress.dismiss();
//        if (pro == 100)
//            if (httpDialog != null) {
//                httpDialog.dismiss();
//            }
    }


    private void newDialog(String message) {
        if (helper == null) helper = new K12DialogHelper(getActivity());

    /* if (helper.getContext().equals(message) && helper.isShow()) {
      return;
    }*/
    }

    public void showDialogMessage(String res_icon, String message) {
        newDialog(message);
        helper.setContent(message);
        helper.setIcon(res_icon);
        helper.show();
    }

    public void showDialogMessage(String message) {
//        newDialog(message);
//        helper.setContent(message);
//        helper.setIcon(getResources().getString(R.string.icon_warning));
//        helper.show();
    }

    public void showDialogMessage(String message, int res) {
        newDialog(message);
        helper.setContent(message);
        helper.setIcon(getResources().getString(res));
        helper.show();
    }


    @UiThread
    public void toast(String msg) {
        if (msg.isEmpty()) {
            return;
        }
        ToastUtils.showShort(msg);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (helper != null) {
            helper.dismiss();
        }
        dismissProgress();
        ToastUtils.cancel();
        super.onDestroy();
    }

    /**
     * 网络请求完毕
     */
    public void onCompelet() {
    }

    public boolean onBackPressed() {
        return false;
    }

}
